package com.briup.bms.web.controller;

import com.briup.bms.bean.Department;
import com.briup.bms.common.utils.Result;
import com.briup.bms.service.IDepartService;
import com.briup.bms.web.vm.DeptParamVM;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Api(tags = "部门信息服务")
@RestController
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private IDepartService deptService;


    @ApiOperation(value = "保存或更新部门信息")
    @PostMapping
    public Result<String> saveOrUpdate(@RequestBody DeptParamVM dept) {
        System.out.println("dept" + dept);
        //业务处理
        deptService.saveOrUpdate(dept);
        return Result.success("操作成功！");
    }

    @GetMapping("/by/deptId")
    public Result<Department> findBydeptId(Integer deptId) {
        return Result.success(deptService.findBydeptId(deptId));
    }

    @GetMapping("/by/page")
    public Result<PageInfo<Department>> findByPageInfo(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize) {
        return Result.success(deptService.findByPageInfo(pageNum, pageSize));
    }

    @ApiOperation(value = "分页模糊查找部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", defaultValue = "5"),
            @ApiImplicitParam(name = "name", value = "部门名称"),
            @ApiImplicitParam(name = "regionId", value = "区域id")
    })
    @GetMapping("/by/condition")
    public Result<PageInfo<Department>> findByCondition(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
            String name, Integer regionId) {

        return Result.success(
                deptService.findByCondition(
                        pageNum, pageSize, name, regionId));
    }

    @DeleteMapping("/{department}")
    public Result<String> deleteDepartment(@PathVariable("department") String department) {
        deptService.deleteDepartment(department);
        return Result.success("删除"+department+"成功");
    }
    @DeleteMapping("/batchDelete")
    public Result<String> batchDeleteCustomer(@RequestParam("departmentIds") Set<Integer> departmentIds) {
        deptService.batchDeleteDept(departmentIds);
//        System.out.println(Arrays.toString(departmentIds));
        return Result.success("删除"+ departmentIds +"成功");
    }
}
